package qc.module.demo.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.demo.dto.student.ScoreDto;
import qc.module.demo.entity.HandleScore;
import qc.module.demo.entity.OriginalScore;
import qc.module.demo.entity.Score;
import qc.module.demo.repository.HandleScoreRepository;
import qc.module.demo.repository.OriginalScoreRepository;
import qc.module.demo.repository.ScoreRepository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ScoreService {

    private ScoreRepository repository;

    @Autowired
    public void setRepository(ScoreRepository repository) {
        this.repository = repository;
    }

    private OriginalScoreRepository originalScoreRepository;

    @Autowired
    public void setOriginalScoreRepository(OriginalScoreRepository originalScoreRepository) {
        this.originalScoreRepository = originalScoreRepository;
    }

    private HandleScoreRepository handleScoreRepository;

    @Autowired
    public void setHandleScoreRepository(HandleScoreRepository handleScoreRepository) {
        this.handleScoreRepository = handleScoreRepository;
    }

    /**
     * 根据学号查询学生成绩
     *
     * @param studentNo 学号
     * @return 没有返回null，学生成绩集合
     * @author cgy
     * @since 2023/6/20
     */
    public List<ScoreDto> queryByStudentNo(Integer studentNo) {

        List<Score> scoreList = repository.queryByStudentNo(studentNo);

        List<ScoreDto> dtoList = new ArrayList<>(scoreList.size());
        if (scoreList != null && scoreList.size() > 0x0) {
            scoreList.forEach(score -> {
                ScoreDto scoreDto = new ScoreDto();
                BeanUtils.copyProperties(score, scoreDto);
                dtoList.add(scoreDto);
            });

            return dtoList;
        }

        return null;
    }

    /**
     * 保存原始成绩数据
     *
     * @param originalScore
     * @return
     */
    public String addOriginalScore(OriginalScore originalScore) {
        originalScore.setDatetime(new Date());
        if (originalScoreRepository.insert(originalScore) < 0x1)
            return QCUnifyReturnValue.Warn("原始数据保存失败");
        return QCUnifyReturnValue.Success();
    }

    /**
     * 保存处理好的数据
     *
     * @param handleScore
     * @return
     */
    public String addHandleScore(HandleScore handleScore) {
        if (handleScoreRepository.insert(handleScore) < 0x1)
            return QCUnifyReturnValue.Warn("处理后的数据保存失败");
        return QCUnifyReturnValue.Success();
    }

}
